<template>
  <div>
    <van-button>
      弹窗测试
    </van-button>
  </div>
</template>

<script setup>
import { useRoute } from 'vue-router';
import {onMounted} from "vue";
import {PayService} from "@/api/api";
import {closeToast, showLoadingToast} from "vant";
import router from "@/router";

const route = useRoute();

onMounted(async () => {
  const params = getURLParameters(route.fullPath);
  console.log(params);
  const data = {
    out_trade_no: params.out_trade_no,
  };
  console.log(data);
  const response = await PayService.getResult(data); // 调用PayService的pay方法，并传入payInfo参数
  console.log(response.data);
  closeToast();
  if(response.data !== -1){
    try {
      const data = {
        appointmentId: response.data
      };

      await router.push({
        path: '/AppointmentDetail',
        state: data
      });
    } catch (error) {
      console.error(error);
    }
  }
});

showLoadingToast({
  message: '验证支付中...',
  forbidClick: true,
  duration: 0
});

function getURLParameters(url) {
  const params = {};
  const paramStr = url.split('?')[1];
  if (paramStr) {
    const paramPairs = paramStr.split('&');
    for (let i = 0; i < paramPairs.length; i++) {
      const pair = paramPairs[i].split('=');
      const key = decodeURIComponent(pair[0]);
      const value = decodeURIComponent(pair[1]);
      params[key] = value;
    }
  }
  return params;
}
</script>
